package org.csu.mypetstore.api.controller.front;

import org.csu.mypetstore.api.common.CommonResponse;
import org.csu.mypetstore.api.entity.Category;
import org.csu.mypetstore.api.entity.Product;
import org.csu.mypetstore.api.service.CatalogService;
import org.csu.mypetstore.api.vo.ItemVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Controller
@RequestMapping("/catalog/")
public class CatalogController {

    @Autowired
    private CatalogService catalogService;

    //直接返回所有categories
    @GetMapping("categories")
    @ResponseBody
    public CommonResponse<List<Category>> getCategoryList(){
        return catalogService.getCategoryList();
    }

    //根据categories的id返回categories
    @GetMapping("categories/{id}")
    @ResponseBody
    public CommonResponse<Category> getCategoryByCategoryId(@PathVariable("id") String categoryId){
        return catalogService.getCategoryByCategoryId(categoryId);
    }

    //根据categories的id返回其下的product
    @GetMapping("categories/{id}/products")
    @ResponseBody
    public CommonResponse<List<Product>> getProductListByCategoryId(@PathVariable("id") String categoryId){
        return catalogService.getProductListByCategoryId(categoryId);
    }

    //直接返回所有product
    @GetMapping("products")
    @ResponseBody
    public CommonResponse<List<Product>> getProductList(){
        return catalogService.getProduct();
    }

    //根据id返回product
    @GetMapping("products/{id}")
    @ResponseBody
    public CommonResponse<Product> getProductByProductId(@PathVariable("id") String productId){
        return catalogService.getProductByProductId(productId);
    }

    //根据product的id返回其下的item
    @GetMapping("products/{id}/items")
    @ResponseBody
    public CommonResponse<List<ItemVO>> getItemsByProductId(@PathVariable("id") String productId){
        return catalogService.getItemsByProductId(productId);
    }

    // 根据item的id获取单个Item商品的信息
    @GetMapping("items/{id}")
    @ResponseBody
    public CommonResponse<ItemVO> getItemByItemId(@PathVariable("id") String itemId){
        return catalogService.getItemByItemId(itemId);
    }

    // 获取所有Items的信息
    @GetMapping("items")
    @ResponseBody
    public CommonResponse<List<ItemVO> > getItemList() {
        return catalogService.getItems();
    }
   //根据name获取商品
    @GetMapping("search/product/{id}")
    @ResponseBody
    public CommonResponse<List<Product>> getProductByName(@PathVariable("id") String name) {
        return catalogService.getProductByName(name);
    }

}
